package com.wangxiaobao.charge.mylibrary.response;

import com.wangxiaobao.charge.mylibrary.utils.HexUtil;

import java.util.Arrays;

public class ResponseData {

    private int opHead;
    private int ctl;
    private int errorCode;
    private byte[] data;
    public ResponseData(int opHead,int ctl, int errorCode, byte[] data) {
        // h  c  err len   data
        // ef 01 00 11 5758425930333032313132303430303234 2020202020202020202020
        this.opHead = opHead;
        this.ctl=ctl;
        this.errorCode = errorCode;
        this.data = data;
    }

    public int getOpHead() {
        return opHead;
    }

    public int getCtl() {
        return ctl;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public byte[] getData() {
        return data;
    }

    public static ResponseData fromValue(byte[] payload) {
        int opHead = payload[0];
        int ctl = payload[1];
        int errorCode = payload[2];
        int payloadLen = payload[3]==0?0:(byte) (0x00ff & payload[3]);

        return new ResponseData(
                opHead,
                ctl,
                errorCode,
                Arrays.copyOfRange(payload, 4, 4+payloadLen)
        );
    }

    @Override
    public String toString() {
        return "ResponseData{" +
                "opHead=" + opHead +
                ", ctl=" + ctl +
                ", errorCode=" + errorCode +
                ", data=" + HexUtil.printBuffer(data) +
                '}';
    }
}
